using UnityEngine;

public class MeleeWeapon : MonoBehaviour
{
	public GameObject rightHandHandle;

	public GameObject leftHandHandle;

	public Animator animator;

	public float damage = 20f;

	public Player player;

	public float range = 0.5f;

	public float decapitationChance = 0.2f;

	public float knockbackPower;

	public AudioSource audioSource;

	public int bleedingCount = 1;

	public int hitMax = 1;

	private int hitCount;

	public AudioClip[] onHitAudio;

	public AudioClip startAudio;

	public bool hideWhenSleep;

	public float flummoxChance;

	public float flummoxMinTime;

	public float flummoxMaxTime;

	private void Awake()
	{
		Sleep();
	}

	public void Weakup()
	{
		audioSource.enabled = true;
		if ((bool)startAudio)
		{
			audioSource.PlayOneShot(startAudio);
		}
		if (hideWhenSleep)
		{
			base.transform.Find("Body").gameObject.SetActive(value: true);
		}
	}

	public void Sleep()
	{
		audioSource.enabled = false;
		base.transform.localPosition = Vector2.zero;
		base.transform.localRotation = Quaternion.Euler(0f, 0f, 0f);
		if (hideWhenSleep)
		{
			base.transform.Find("Body").gameObject.SetActive(value: false);
		}
	}

	public void PlayOneShot(AudioClip audioClip)
	{
		audioSource.PlayOneShot(audioClip);
	}

	public void Attack()
	{
		hitCount = 0;
		animator.SetTrigger("attack");
	}

	private void OnTriggerEnter2D(Collider2D collision)
	{
		if (!player || (!(collision.name == "HeadRb") && !(collision.name == "ChestRb")))
		{
			return;
		}
		hitCount++;
		if (hitCount > hitMax)
		{
			return;
		}
		BodyPart component = collision.GetComponent<BodyPart>();
		if (!component || Mathf.Abs(component.body.zombie.transform.position.y - player.transform.position.y) > range)
		{
			return;
		}
		component.body.health -= damage * (1f + player.strength / 10f);
		for (int i = 0; i < bleedingCount; i++)
		{
			component.Bleed(component.transform.position);
		}
		if (onHitAudio.Length != 0)
		{
			audioSource.PlayOneShot(onHitAudio[Random.Range(0, onHitAudio.Length)]);
		}
		if (component.body.health <= 0f)
		{
			component.body.Die();
		}
		else if (collision.name == "HeadRb" && decapitationChance >= Random.value)
		{
			component.body.Die();
			Object.Destroy(component.hj);
			if (knockbackPower > 50f && Random.Range(0, 2) > 0)
			{
				component.Explode(component.transform.position);
			}
		}
		if (knockbackPower > 0f)
		{
			float num = knockbackPower * (1f + player.strength / 5f);
			if (!component.body.isDead)
			{
				component.body.zombie.rb.AddForce(num * 10f * Vector2.right);
			}
			else
			{
				Vector2 vector = Vector2.right;
				if (base.gameObject.name == "BaseballBat")
				{
					vector = new Vector2(1f, 0.5f);
				}
				component.rb.AddForce(num * vector);
				component.rb.AddTorque(-2f);
				component.body.transform.Find("ChestRb").GetComponent<Rigidbody2D>().AddForce(num * 2f * Vector2.right);
			}
		}
		if (component.body.zombie.animator.GetCurrentAnimatorStateInfo(0).IsName("ZombieVomit"))
		{
			component.body.zombie.animator.CrossFade("ZombieVomitCancel", 0.05f);
		}
		component.body.Speak(component.body.hurtAudios[Random.Range(0, component.body.hurtAudios.Length)]);
		if (flummoxChance > 0f && flummoxChance > Random.Range(0f, 1f))
		{
			component.body.zombie.flummoxUntil = Time.time + Random.Range(flummoxMinTime, flummoxMaxTime);
		}
	}
}
